package com.megagoodsoftware.smugmug;

import java.util.*;


import android.os.Parcel;
import android.os.Parcelable;


public class SmugMugAlbum implements Parcelable {

	private String title;
	private int id;
	private ArrayList<SmugMugImage> images;
	private String key;
	private String category;

	public SmugMugAlbum(int id, String key, String title) {
		this.id = id;
		this.key = key;
		this.title = title;
		images = new ArrayList<SmugMugImage>();
	}

	@SuppressWarnings("unchecked")
	public SmugMugAlbum(Parcel in) {
		id = in.readInt();
		key = in.readString();
		title = in.readString();
		images = new ArrayList<SmugMugImage>();
		in.readTypedList((List)images, CREATOR);
	}

	public String getTitle() {
		return title;
	}

	public int getId() {
		return id;
	}

	public boolean hasImage(SmugMugImage image) {
		return images.contains(image);
	}

	public void addImage(SmugMugImage image) {
		images.add(image);
	}

	public String getKey() {
		return this.key;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public ArrayList<SmugMugImage> getImages() {
		return images;
	}

	public void clearImages() {
		images.clear();
	}

	/**
     * Retrieves a list of photos. The list contains at most the
     * number of photos specified by <code>perPage</code>. The photos are retrieved
     * starting a the specified page index. For instance, if a user has 10 photos in
     * his photostream, calling getPublicPhotos(5, 2) will return the last 5 photos
     * of the photo stream.
     *
     * The page index starts at 1, not 0.
     *
     * @param perPage The maximum number of photos to retrieve.
     * @param page The index (starting at 1) of the page in the photostream.
     *
     * @return A list of at most perPage photos.
     *
     * @see com.megagoodsoftware.smugmug.google.android.photostream.Flickr.Photo
     * @see com.megagoodsoftware.smugmug.google.android.photostream.Flickr.PhotoList
     * @see #downloadPhoto(com.megagoodsoftware.smugmug.google.android.photostream.Flickr.Photo,
     *          com.megagoodsoftware.smugmug.google.android.photostream.Flickr.PhotoSize, java.io.OutputStream) 
     */

	public PhotoList getImages(int photosCountPerPage, Integer currentPage) {
		PhotoList pList = new PhotoList();
		pList.setPage(currentPage);
		pList.setPageCount(photosCountPerPage);
		pList.setPhotos(new ArrayList<Photo>());
		
		int firstImage = 0;
		int lastImage = images.size() - 1;
		
		if (photosCountPerPage != 0) {
			firstImage = (currentPage - 1) * photosCountPerPage;
			lastImage = firstImage + photosCountPerPage - 1;
		}

		for (int i = firstImage; i <= lastImage && i < images.size(); i++) {
			Photo photo = new Photo();
			SmugMugImage image = images.get(i);
			photo.setId(Integer.toString(image.getId()));
			photo.setTitle(image.getName());
			photo.setSmallUrl(image.getTinyUrl());
			photo.setMediumUrl(image.getMediumUrl());
			pList.add(photo);
		}
			
		return pList;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
	       dest.writeInt(getId());
	        dest.writeString(getKey());
	        dest.writeString(getTitle());
	        dest.writeTypedList(getImages());
	 	
	}
    public static final Parcelable.Creator<SmugMugAlbum> CREATOR = new Parcelable.Creator<SmugMugAlbum>() {
        public SmugMugAlbum createFromParcel(Parcel in) {
            return new SmugMugAlbum(in);
        }

        public SmugMugAlbum[] newArray(int size) {
            return new SmugMugAlbum[size];
        }
    };
}
